package jsontest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class JsonDemo1 {
    public static void main4(String[] args) throws Exception {
        // 使用JSON对象 将java对象数组写入 JSON 文件
        Person[] people = {new Person("张三", 18), new Person("王五", 19), new Person("赵六", 20)};
        // 将person.json 文件读取为字符串
        BufferedReader br1 = new BufferedReader(new FileReader("day14-code/src/json/person.json"));
        StringBuffer sb = new StringBuffer();
        String s1 = null;
        while ((s1 = br1.readLine()) != null) {
            sb.append(s1.trim());
        }
        br1.close();

        // 获取到文件中的JSON格式的字符串
        String json = sb.toString();
        System.out.println(json);
        // 将JSON格式的字符串 转换成java中的集合对象
        List<Person> peopleList = JSON.parseArray(json, Person.class);
        System.out.println("java集合：" + peopleList);

        // 先将json格式字符串转换成JSONArray对象，再将JSONArray对象转换成java集合对象
        JSONArray jsonArray = JSON.parseArray(json);
        List<Person> people1 = jsonArray.toJavaList(Person.class);
        System.out.println("java集合：" + people1);

    }

    public static void main2(String[] args) {

        //将person对象转换成JSON对象
        Person person = new Person("张三", 18);
        //2.将person对象转换成字符串
        String s1 = person.toString();
        //3.将person转换成JSON对象
        JSONObject json = (JSONObject) JSON.toJSON(person);
        String name = (String) json.get("name");
        System.out.println("name:" + name);
        Integer age = (Integer) json.get("age");
        System.out.println("age:" + age);
        //将数组转换成JSON对象
        List<Person> people = Arrays.asList(new Person("张三", 18), new Person("王五", 19), new Person("赵六", 20));
        JSONArray json1 = (JSONArray) JSON.toJSON(people);
        // 将JSON对象转换成字符串
        String s = json1.toString();
        System.out.println(s);

    }

    public static void main1(String[] args) {
        //使用 JSON 创建 JSON 对象,将java对象转换成 JSON 对象
        Person person = new Person("帅个", 18);
        System.out.println(person);
        // 将java对象转换成字符串
        JSONObject jsonObject = (JSONObject) JSON.toJSON(person);
        //将JSON对象转换成字符串
        String s = jsonObject.toString();
        System.out.println(s);
        //将字符串转换成JSON对象
        JSONObject parse = (JSONObject) JSON.parse(s);
        JSONObject jsonObject1 = JSON.parseObject(s);
        //将JSON对象转换成字符串
        String s1 = jsonObject1.toString();
        System.out.println(s1);

    }

    public static void main(String[] args) {
        //使用 JSON 对象，将 String 字符串转换成 JSON 对象
        String str = "{\"name\":\"张三\",\"age\":18}";
        JSON json = (JSON) JSON.parse(str);
        System.out.println("json对象" + json);
        //1.使用 JSON 对象，将 JSON 对象转换成 Java 对象 (快捷直接转)
        Person person = JSON.toJavaObject(json, Person.class);// Person.class是模板
        System.out.println("java对象" + person);
    }
}

class Person implements Serializable {
    @Serial
    private static final long serialVersionUID = 2115952952303615668L;
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "姓名：" + name + "，年龄：" + age;
    }
}